// --------------------------------------------------------------------------------------------------------------------
// <copyright file="KukaLBR4RegisterManager.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Hardware.KukaLBR
{
    using System;
    using System.Collections.Generic;

    using Microsoft.Robotics.Manipulation;
    using Microsoft.Robotics.Manipulation.ControllerStateMachine;
    using Microsoft.Robotics.Numerics;

    using LBR4 = Microsoft.Robotics.Hardware.KukaLBR.KukaLBR4PlusCsharpDefinitionsNativeMethods;

    /// <summary>
    /// Class for assigning working registers to functions in state machines
    /// </summary>
    public class KukaLBR4WorkingRegisterManager
    {
        /// <summary>
        /// Lists register identifiers (in the order to be distributed) for each register type.
        /// The types must be in the same order as the <c>RegisterTypes</c> enum.
        /// </summary>
        private Dictionary<RegisterValueTypes, List<LBR4.KukaLBR4PlusWorkingRegisters>> registers =
            new Dictionary<RegisterValueTypes, List<LBR4.KukaLBR4PlusWorkingRegisters>>()
        {
            {
                RegisterValueTypes.Scalar, 
                new List<LBR4.KukaLBR4PlusWorkingRegisters>
                {
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar1Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar2Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar3Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar4Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar5Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar6Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar7Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Scalar8Register
                }
           },
           {
                RegisterValueTypes.CartesianVector,
                new List<LBR4.KukaLBR4PlusWorkingRegisters>()
                {
                    LBR4.KukaLBR4PlusWorkingRegisters.CartVec1Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.CartVec2Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.CartVec3Register
                }
           },
           {
                RegisterValueTypes.JointVector,
                new List<LBR4.KukaLBR4PlusWorkingRegisters>()
                {
                    LBR4.KukaLBR4PlusWorkingRegisters.JointVec1Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.JointVec2Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.JointVec3Register
                }
            },
            {
                RegisterValueTypes.Frame,
                new List<LBR4.KukaLBR4PlusWorkingRegisters>()
                {
                    LBR4.KukaLBR4PlusWorkingRegisters.Frame1Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Frame2Register,
                    LBR4.KukaLBR4PlusWorkingRegisters.Frame3Register
                }
            }
        };
   
        /// <summary>
        /// Initializes a new instance of the <see cref="KukaLBR4WorkingRegisterManager"/> class.
        /// </summary>
        public KukaLBR4WorkingRegisterManager()
        {
            this.CurrentRegisterAvailableCount = new Dictionary<RegisterValueTypes, int>();
            this.CurrentRegisterAvailableCount.Add(RegisterValueTypes.Scalar, this.registers[RegisterValueTypes.Scalar].Count);
            this.CurrentRegisterAvailableCount.Add(RegisterValueTypes.CartesianVector, this.registers[RegisterValueTypes.CartesianVector].Count);
            this.CurrentRegisterAvailableCount.Add(RegisterValueTypes.JointVector, this.registers[RegisterValueTypes.JointVector].Count);
            this.CurrentRegisterAvailableCount.Add(RegisterValueTypes.Frame, this.registers[RegisterValueTypes.Frame].Count);
        }

        /// <summary>
        /// Types of registers assigned by the register manager
        /// </summary>
        public enum RegisterValueTypes
        {
            /// <summary>
            /// Scalar registers
            /// </summary>
            Scalar,

            /// <summary>
            /// Cartesian vector registers
            /// </summary>
            CartesianVector,

            /// <summary>
            /// Joint vector registers
            /// </summary>
            JointVector,

            /// <summary>
            /// Frame registers
            /// </summary>
            Frame,
        }

        /// <summary>
        /// Gets number of remaining registers of each type available for assignment
        /// </summary>
        public Dictionary<RegisterValueTypes, int> CurrentRegisterAvailableCount { get; private set; }

        /// <summary>
        /// Gets the ID of the next available register of the indicated type
        /// </summary>
        /// <param name="type">Type of register to get.</param>
        /// <returns>A register ID.</returns>
        public int GetNextRegisterID(RegisterValueTypes type)
        {
            if (--this.CurrentRegisterAvailableCount[type] < 0)
            {
                throw new ApplicationException("No more registers of type: " + type + " are available.");
            }

            return (int)this.registers[type][this.registers[type].Count - this.CurrentRegisterAvailableCount[type] - 1];
        }
    }
}
